﻿using DevLib.TestCase;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


/** 
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
 * Description: 
 * Author: Heller (HellerSong@outlook.com)
 * CreationTime: 3/24/2015 10:09:17 PM
 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 
 **/
namespace DevLib.SuperTool
{
	public class StringOM
	{
		private StringOM() { }

		public static Boolean IsEmpty(String str)
		{
			if (str.Length == 0)
				return true;
			else
				return false;
		}

		public static String UpperWordFirstLetter(String word)
		{
			StrCheck.CheckChainArgUsable(word);

			String temp = word.ToLower();
			return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(temp);
		}
		public static String UpperWordAllLetters(String word)
		{
			return word.ToUpper();
		}
		public static String UpperSentenceToTitleCase(String sentence)
		{
			return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(sentence);
		}
		public static String UpperSentenceFirstLetter(String sentence)
		{
			char[] allChars = sentence.ToLower().ToCharArray();
			allChars[0] = allChars[0].ToString().ToUpper().ToCharArray()[0];

			return new String(allChars);
		}

		public static String ConvertToLowerCamelCase(String sourceString)
		{
			StrCheck.CheckChainArgUsable(sourceString);

			String result = null;

			String[] words = DevRegex.GetMatchedStringArray(sourceString, DevRegex.MatchWord);
			// The first word in a special treatment, lower, others upper
			result += words[0].ToLower();
			// Left words all to upper
			for (int i = 1; i < words.Length; i++)
				result += UpperWordFirstLetter(words[i]);

			return result;
		}
		public static String ConvertToUpperCamelCase(String sourceString)
		{
			StrCheck.CheckChainArgUsable(sourceString);

			String result = null;

			String[] words = DevRegex.GetMatchedStringArray(sourceString, DevRegex.MatchWord);
			// Upper all words
			foreach (String s in words)
				result += UpperWordFirstLetter(s);

			return result;
		}
		public static String ConvertToUnixCase(String sourceString)
		{
			StrCheck.CheckChainArgUsable(sourceString);

			String result = null;

			// Appent "_" between each word
			String[] words = DevRegex.GetMatchedStringArray(sourceString, DevRegex.MatchWord);
			for (int i = 0; i < words.Length; i++)
				result += words[i].ToLower() + "_";
			// Remove the tail char "_"
			result = result.Remove(result.Length - 1);

			return result;
		}

		public static int GetCharIndexFirstAppear(char targetChar, String sourceString)
		{
			int resultIndex = -1;

			char[] charArray = sourceString.ToCharArray();
			for (int i = 0; i < charArray.Length; i++)
				if (charArray[i] == targetChar)
					return resultIndex = i;

			return resultIndex;
		}
		public static int GetCharIndexSecondAppear(String sourceString, char targetChar, int appearCount)
		{
			int resultIndex = -1;

			char[] charArray = sourceString.ToCharArray();
			for (int i = 0; i < charArray.Length; i++)
			{
				if (charArray[i] == targetChar)
					if (resultIndex == -1)  // First appear
						resultIndex = i;
					else
						return resultIndex = i; // Second appear
			}

			return resultIndex;
		}
		public static int GetCharIndexLastAppear(char targetChar, String sourceString)
		{
			int resultIndex = -1;

			char[] charArray = sourceString.ToCharArray();
			for (int i = charArray.Length - 1; i > -1; i--)
				if (charArray[i] == targetChar)
					return resultIndex = i;

			return resultIndex;
		}

		public static Dictionary<String, int> CountWords(String filePath)
		{
			Dictionary<String, int> allWords = new Dictionary<String, int>();

			using (StreamReader sr = new StreamReader(filePath, Encoding.UTF8))
			{
				String line = null;
				String[] words = null;

				while ((line = sr.ReadLine()) != null)
				{
					line = line.ToLower();
					if (line.Length > 0)
					{
						words = DevRegex.GetMatchedStringArray(line, DevRegex.MatchWord);
						if (words != null && words.Length > 0)
						{
							for (int i = 0; i < words.Length; i++)
							{
								if (allWords.ContainsKey(words[i]))
									allWords[words[i]]++;
								else
									allWords.Add(words[i], 1);
							}
						}
					}
				}
			}

			// Sort the dictionary by value
			allWords = allWords.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);

			return allWords;
		}

	}
}
